var sname = "";//树形结构的选中值 树形结构ID
//初始加载
$(document).ready(function(){
    queryZyTree();//获取树形结构
    $("#table_list").css("height",($("body").height()-120));
});

//值改变事件
// $("#zmc").change(function(){
//     var zmc = $("#zmc").val();
//     if(sjymc!=null && sjymc != '') {
//         queryDataSource(zmc);
//     }else{
//         queryDataSource("");
//     }
// });

//键盘事件 查询
// $('#zmc').bind('keydown',function(event){
//     var zmc = $("#zmc").val();
//     if(zmc!=null && zmc != ''){
//         queryDataSource(zmc);
//     }else{
//         queryDataSource("");
//     }
// });

//按钮搜索事件
function queryByParam() {
    var zmc = $("#zmc").val();
    if(zmc!=null && zmc != ''){
        queryDataSource(zmc);
    }else{
        queryDataSource("");
    }
}

//查询值域树
function queryZyTree(){
    $.ajax({
        type:"GET",
        dataType:"json",
        url:"zygl/queryZyTree",
        success:function(jsonData){
            if(jsonData.code == 200){
                initJgTree(jsonData.list);
            }
        }
    });
}
//添加和编译值域目录后的回调
function callbackTree(zydm){
    sname=zydm;
    queryZyTree();
}

//初始化加载树
function initJgTree(jsonTreeData){
    $.fn.zTree.init($('#table_list'),setting,jsonTreeData);
    fuzzySearch('table_list','#zymc',null,true);
    var treeObj = $.fn.zTree.getZTreeObj("table_list");
    var nodes = treeObj.getNodes();
    if(nodes.length>0){
        if(sname!='' && sname!=null){
            var node = treeObj.getNodeByParam("id",sname);
            treeObj.selectNode(node,true);//将指定ID的节点选中
            treeObj.expandNode(node,true,true,true);
        }else{
            var node=nodes[0];
            if(node.children.length>0){
                node=nodes[0].children;
            }
            sname = node[0].id;
            treeObj.selectNode(node[0]);
            treeObj.expandNode(node[0],true,true,true);
        }
    }
    $("#zmc").val("");
    queryDataSource("");
}

var setting = {//树形配置
    view: {
        fontCss: {"font-family":"Microsoft YaHei"},
        selectedMulti: false,
        showIcon:false
    },
    edit: {
        enable: true,
        removeTitle:"删除",
        renameTitle:"修改",
        showRemoveBtn: showRemoveBtn,
        showRenameBtn: showRenameBtn
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        onClick: zTreeOnClick,
        beforeEditName: beforeEditName,
        beforeRemove: beforeRemove
    }
};
//判断是否显示修改图标
function showRenameBtn(treeId, treeNode) {
    if(treeNode.pId==null || treeNode.pId=="" || treeNode.pId==undefined){
        return false;
    }
    return true;
}
//判断是否显示删除图标
function showRemoveBtn(treeId, treeNode) {
    if(treeNode.pId==null || treeNode.pId=="" || treeNode.pId==undefined){
        return false;
    }
    return true;
}
//树形结构编辑
function beforeEditName(treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj(treeId);
    zTree.selectNode(treeNode);
    sname=treeNode.id;
    treeClick(treeNode.id,treeNode.pId);
    updateZy(treeNode.id);
    return false;
}
//树形结构删除
function beforeRemove(treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj(treeId);
    zTree.selectNode(treeNode);
    sname=treeNode.id;
    treeClick(treeNode.id,treeNode.pId);
    LayerWn.showConfirm("确定要删除吗？", function () {
        var queryParms = {zydm:sname};
        $.ajax({
            type: "GET",
            contentType: 'application/json',
            dataType: "json",
            url: "zygl/delZy",
            data: queryParms,
            success: function (jsonData) {
                if (jsonData.code == 200) {
                    LayerWn.showSucAlert("删除成功");
                    callbackTree("");
                } else {}
            }
        });
    });
    return false;
}
//树点击事件-共同
function treeClick(id,pId) {
    sname = id;
    $("#zmc").val("");
    if(pId==null || pId=='' || pId==undefined){
        queryDataSourceMl(sname);//通过值域目录查询
    }else{
        queryDataSource("");//通过值域查询
    }
}
//点击树操作
function zTreeOnClick(event,treeId,treeNode){
    treeClick(treeNode.id,treeNode.pId);
}
//查询表格数据 通过值域
function queryDataSource(zmc){
    //var param = {zydm:sname,zmc:zmc};
    var param = {zydm:sname};
    showTable(param);
}
//查询表格数据 通过值域目录
function queryDataSourceMl(name) {
    var param = {zylbsm:name};
    showTable(param);
}
//表格渲染
function showTable(data){
    var url = "zygl/queryList";
    var columnList = [
        {
            field: 'zdm',
            title: '值代码',
            align: 'left',
            halign: 'left',
            valign: 'middle',
            width: '180'
        },
        {
            field: 'zmc',
            title: '值名称',
            align: 'left',
            halign: 'left',
            valign: 'middle',
            width: '180'
        },{
            field: 'zsm',
            title: '值说明',
            align: 'left',
            halign: 'left',
            valign: 'middle',
            width: '180',
            visible:true
        },{
            field: 'zoid',
            title: '值OID',
            align: 'left',
            halign: 'left',
            valign: 'middle',
            width: '180',
            visible:false
        },
        {
            field: 'zcwj',
            title: '政策文件',
            align: 'left',
            halign: 'left',
            valign: 'middle',
            width: '180',
            visible:false
        },
        {
            field: 'jlzt',
            title: '状态',
            align: 'left',
            halign: 'left',
            valign: 'middle',
            width: '80'  ,
            formatter: function (value, row, index) {
                if(value == "0"){
                    return "<span style='color: #07BFA7;'> 启用 </span>";
                }else if(value == "1"){
                    return "<span style='color: red;'> 停用 </span>";
                }
            }
        },
        {
            field: '',
            title: '操作',
            align: 'left',
            halign: 'left',
            valign: 'middle',
            width: '100',
            formatter: function (value,row) {
                var zdm = row.zdm;
                var zydm = row.zydm;
                return '<div>' +
                    '<a class="blue" href="#" onclick="updateZyx(\''+zdm+'\',\''+zydm+'\')">' +
                    '<i class="btn-eidt-wn" style="font-size: 125%" title="修改"></i></a>' +
                    '<a class="red" href="#" onclick="del(\''+zdm+'\',\''+zydm+'\')">' +
                    '<i class="btn-del-wn" style="font-size: 125%" title="删除"></i></a></div>' ;
            }
        }
    ];
    $("#datasetTab").bootstrapTable('destroy').bootstrapTable({
        url:url,
        method:'POST',
        dataType:'json',
        striped: true,                      //是否显示行间隔色
        cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        sortable: false,                     //是否启用排序
        height:$(document).height() - 90,
        searchAlign: 'left',
        onClickRow: function (row, element, field) {
            $('.info').removeClass('info');//去除之前选中的行的，选中样式
            $(element).addClass('info');//添加当前选中的 success样式用于区别
        },
        columns: columnList,
        pagination: true,                   //是否显示分页（*）
        pageSize: 10,
        pageList: [10, 20, 50, 100],
        sidePagination:"server",
        queryParamsType:"",
        queryParams:function queryParams(params){//设置查询参数
            var param = data;
            var PageBean = {
                pageNo : params.pageNumber,//页码
                pageSize : params.pageSize,//每页多少条数据
                totalRecord:0,
                totalPage:0,
                results:[],
                orderColumn:'order by t1.zydm,t1.zdm asc'
            };
            param.page = PageBean;
            param = JSON.stringify(param);
            return param;
        }
    });
}
//删除值域项
function del(zdm,zydm) {
    var queryParms = {zdm: zdm,zydm:zydm};
    LayerWn.showConfirm("确定要删除吗？", function () {
        $.ajax({
            type: "GET",
            contentType: 'application/json',
            dataType: "json",
            url: "zygl/delZyx",
            data: queryParms,
            success: function (jsonData) {
                if (jsonData.code == 200) {
                    LayerWn.showSucAlert("删除成功");
                    queryDataSource();
                } else {

                }
            }
        });
    });
}
//新增值域项
function addZyx() {
    if(sname==''){
        LayerWn.showAlert("请先选择值域");
        return false;
    }else{
        var zTree = $.fn.zTree.getZTreeObj("table_list");
        var node = zTree.getNodeByParam("id",sname );
        if(node.pId=='' || node.pId==null || node.pId==undefined){
            LayerWn.showAlert("请选择值域");
            return false;
        }
        LayerWn.openDialog('新增值域项', encodeURI(encodeURI('psms-zyx-add.html?zydm='+sname)), 800, 480);
    }
}
//更新值域项
function updateZyx(zdm,zydm) {
    LayerWn.openDialog('修改值域项', 'psms-zyx-add.html?zclsh=update&zdm='+zdm+'&zydm='+zydm,800, 480);
}
//更新值域
function addZy() {
    LayerWn.openDialog('新增值域目录', encodeURI(encodeURI('psms-zyml-add.html')), 800, 480);
}
//更新值域
function updateZy(value) {
    LayerWn.openDialog('修改值域目录', 'psms-zyml-add.html?zclsh=update&zydm='+value,800, 480);
}
